//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_kehflogic.sqf"
#include "x_setup.sqf"

private ["_killed", "_kgrp", "_var", "_type", "_kdata"];
PARAMS_1(_killed);
_var = _killed getVariable QGVAR(hq_logic_name);
_side = _killed getVariable QGVAR(hq_logic_side);
_kdata = _killed getVariable QGVAR(kddata);
_type = typeOf _killed;
_kgrp = group _killed;
deleteVehicle _killed;
if (isNull _kgrp) then {
	_kgrp = [_side] call FUNC(creategroup);
};
_unit = _kgrp createUnit [_type,[0,0,0],[],0,"NONE"];
missionNamespace setVariable [_var, _unit];
[_unit] joinSilent _kgrp;
_unit addEventHandler ["handleDamage",{0}];
_unit enableSimulation false;
_unit hideObject true;
_unit addItem "ItemRadio";
_unit assignItem "ItemRadio";
publicVariable _var;
_unit setVariable [QGVAR(hq_logic_name), _var];
_unit setVariable [QGVAR(hq_logic_side), _side];
_unit setVariable [QGVAR(kddata), _kdata];

{
	_unit kbAddTopic[_x,"x_bikb\domkba3.bikb"];
} forEach (_kdata select 0);
_unit setIdentity (_kdata select 1);
_unit setRank "COLONEL";
_unit setGroupId [_kdata select 2];
_unit addEventHandler ["killed", {_this call FUNC(kEHflogic)}];
removeAllWeapons _unit;
[QGVAR(kbunits), [_var, _side]] call FUNC(NetCallEventToClients);
_unit setVariable [QGVAR(noSimCheck), true];